/*
 * Copyright 2018 Netflix, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package com.netflix.zuul;

import com.google.inject.Injector;
import com.netflix.config.ConfigurationManager;
import com.netflix.governator.InjectorBuilder;
import com.netflix.zuul.netty.server.BaseServerStartup;
import com.netflix.zuul.netty.server.Server;

/**
 * Bootstrap class for Zuul 2 sample.
 */
public class Bootstrap {

  // 发起请求
  // curl -X POST \
  // http://127.0.0.1:8887/ \
  //     -H 'cookie: customer-Id=34bf81cf-843f-4d9f-90a4-2f21afe4f96c'

  public static void main(String[] args) {
    new Bootstrap().start();
  }

  public void start() {
    System.out.println("Zuul 2 Sample: starting up.");
    long startTime = System.currentTimeMillis();
    int exitCode = 0;

    Server server = null;

    try {
      ConfigurationManager.loadCascadedPropertiesFromResources("application");
      Injector injector = InjectorBuilder.fromModule(new ZuulSampleModule()).createInjector();
      BaseServerStartup serverStartup = injector.getInstance(BaseServerStartup.class);
      server = serverStartup.server();

      long startupDuration = System.currentTimeMillis() - startTime;
      System.out.println("Zuul Sample: finished startup. Duration = " + startupDuration + " ms");

      server.start(true);
    } catch (Throwable t) {
      t.printStackTrace();
      System.err.println("###############");
      System.err.println("Zuul Sample: initialization failed. Forcing shutdown now.");
      System.err.println("###############");
      exitCode = 1;
    } finally {
      // server shutdown
      if (server != null) {
        server.stop();
      }

      System.exit(exitCode);
    }
  }
}